<script type="text/javascript" src="<?php echo URL_JS; ?>ckeditor/ckeditor.js"></script>
<script type="text/javascript" src="<?php echo URL_JS; ?>update_news_form.js"></script>
<script type="text/javascript">

</script>
<script type="text/javascript">
    function ClicBouton(url){
        document.form2.submit();

        document.form2.action = url;  
      
    }
</script>
<?php
if (!defined('CONFIGURATION_FILE_LOADED'))
    include('includes/configuration_include.php');
include( 'includes/regions.php');

include( PATH_VIEWS . 'update_news_view.php');
include( PATH_MODELS . 'update_news_model.php');

$bdd = model_db_connexion();
$req = model_news($bdd);
$count_news = count_news($bdd);

// Si l'utilisateur est deja logue
if (!empty($_SESSION['id_user'])) {

    echo'<span class="template_title">Gestion des actualitées</span>';
    
    echo 'Si vos modification ne s\'affiche pas rafraichissez la page(F5)';
    if (isset($_POST['delete'])) {
        $new_id = $_POST['delete'];
        delete_news($bdd, $new_id);
    }
    if (isset($_POST['new_id'])) {
        $new_id = $_POST['new_id'];
        $new_name = $_POST['new_title' . $new_id . ''];
        $new_description = $_POST['description' . $new_id . ''];
        if (isset($_FILES['photo' . $new_id . '']) AND $_FILES['photo' . $new_id . '']['error'] == 0) {
            if (upload_photo_test($new_id) != 3) {
                
                $type_img = upload_photo_test($new_id);
                upload_photo($new_id, $type_img);
                model_update_news($bdd, $new_name, $new_description, $new_id, $type_img);
            } else {
                                model_update_news($bdd, $new_name, $new_description, $new_id, $type_img);

                echo' <span class="template_span">Votre image n\'est pas au bon format</span>';
            }
        } else {
            
                            model_update_news($bdd, $new_name, $new_description, $new_id, 3);

        }
    }

    if (isset($_POST['envoyer'])) {


        if ($_POST['envoyer'] == 'Valider') {
            $req = model_news($bdd);


            while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
                $i = $donnees['id_new'];
                $new_name = $donnees['new_name'];
                $new_description = $donnees['new_description'];
                $new_id = $donnees['id_new'];
                $type_img = $donnees['img_type'];
                if (isset($_FILES['photo' . $i . '']) AND $_FILES['photo' . $i . '']['error'] == 0) {
                    if (upload_photo_test($i) != 3) {
                        
                        $type_img = upload_photo_test($i);
                        upload_photo($i, $type_img);
                      
                        model_update_Allnews($bdd, $new_name, $new_description, $i, $type_img);
                    } else {
                        echo' Le telechargement concernant l\'actualité  ' . $i . ' n\'est pas au bon format';
                    }
                } else {

                }
            }
        }
    }


    start_form();
    while ($donnees = $req->fetch(PDO::FETCH_ASSOC)) {
        $new_name = $donnees['new_name'];
        $new_description = $donnees['new_description'];
        $new_id = $donnees['id_new'];
        $type_img = $donnees['img_type'];
        ?>
        <script type="text/javascript">
                                
            function visu($new_id)
            { var w = $new_id;
                CKEDITOR.replace("visu"+w, 
                {
                    height:"150", width:"900"
                });}

            function envoi_form($new_id){
                var w = $new_id;
                document.form2.action = "<?php echo URL_ROOT . 'update-news/new_id-' ?>"+w;  


                document.form2.submit();
            } 
            function envoi_form_delete($new_id){
                var w = $new_id;
                document.form2.action = "<?php echo URL_ROOT . 'update-news/delete-' ?>"+w;  


                document.form2.submit();
            } 
        </script>
        <?php
        $str = printNews($new_name, $new_description, $new_id, $type_img);
        echo ' <script type="text/javascript"> visu(' . $new_id . ' );</script>';
    }

    end_form();
    ?>



    <?php


  

} else {
    echo '<span class="template_span">' . LANG_NO_CONNECT . '</span>';
}

   function upload_photo($count_news) {

    if (isset($_FILES['photo'.$count_news])) {
        if ($_FILES['photo'.$count_news]['error'] == 0) {

            if ($_FILES['photo'.$count_news]['size'] <= 7000000) {

                $infosfichier = pathinfo($_FILES['photo'.$count_news]['name']);

                $extension_upload = $infosfichier['extension'];
  $extension_upload =strtolower($extension_upload);

            $extensions_autorisees = array('webm', 'jpeg');

                if (in_array($extension_upload, $extensions_autorisees)) {




                    //$name= $id_work .".". $extension_upload;

                    move_uploaded_file($_FILES['photo'.$count_news]['tmp_name'], "design/img/news/" . $count_news . "." . $extension_upload . "");
                    return TRUE;
                } else {
                    echo '<span class="template_span">Votre format de l\'image 1 n\'est pas accepte, veuillez reessayer</span><br />';
                    return FALSE;
                }
            }
        } else {
            return FALSE;
        }
    }
}

function upload_photo_test($i) {
    if (isset($_FILES['photo'.$i])
            && $_FILES['photo'.$i]['error'] == 0) {

        if ($_FILES['photo'.$i]['size'] <= 7000000) {

            $infosfichier = pathinfo($_FILES['photo'.$i]['name']);

            $extension_upload = $infosfichier['extension'];
  $extension_upload =strtolower($extension_upload);

            $extensions_autorisees = array('webm', 'jpeg');

            if (in_array($extension_upload, $extensions_autorisees)) {




                switch ($extension_upload) {
                    case 'webm' :
                        return 0;
                        break;
                    case 'jpeg' :
                        return 1;
                        break;
                                  }
                //$name= $id_work .".". $extension_upload;
            } else {
                return 3;
            }
        }
    } else {
        return FALSE;
    }}

 function fctredimimage($W_max, $H_max, $rep_Dst, $img_Dst, $rep_Src, $img_Src) {
 $condition = 0;
 // Si certains paramètres ont pour valeur '' :
 if ($rep_Dst=='') { $rep_Dst = $rep_Src; } // (même répertoire)
 if ($img_Dst=='') { $img_Dst = $img_Src; } // (même nom)
 // ---------------------
 // si le fichier existe dans le répertoire, on continue...
 if (file_exists($rep_Src.$img_Src) && ($W_max!=0 || $H_max!=0)) { 
   // ----------------------
   // extensions acceptées : 
	$extension_Allowed = 'jpg,jpeg,png';	// (sans espaces)
   // extension fichier Source
	$extension_Src = strtolower(pathinfo($img_Src,PATHINFO_EXTENSION));
   // ----------------------
   // extension OK ? on continue ...
   if(in_array($extension_Src, explode(',', $extension_Allowed))) {
     // ------------------------
      // récupération des dimensions de l'image Src
      $img_size = getimagesize($rep_Src.$img_Src);
      $W_Src = $img_size[0]; // largeur
      $H_Src = $img_size[1]; // hauteur
      // ------------------------
      // condition de redimensionnement et dimensions de l'image finale
      // ------------------------
      // A- LARGEUR ET HAUTEUR maxi fixes
      if ($W_max!=0 && $H_max!=0) {
         $ratiox = $W_Src / $W_max; // ratio en largeur
         $ratioy = $H_Src / $H_max; // ratio en hauteur
         $ratio = max($ratiox,$ratioy); // le plus grand
         $W = $W_Src/$ratio;
         $H = $H_Src/$ratio;   
         $condition = ($W_Src>$W) || ($W_Src>$H); // 1 si vrai (true)
      }
      // ------------------------
      // B- HAUTEUR maxi fixe
      if ($W_max==0 && $H_max!=0) {
         $H = $H_max;
         $W = $H * ($W_Src / $H_Src);
         $condition = ($H_Src > $H_max); // 1 si vrai (true)
      }
      // ------------------------
      // C- LARGEUR maxi fixe
      if ($W_max!=0 && $H_max==0) {
         $W = $W_max;
         $H = $W * ($H_Src / $W_Src);         
         $condition = ($W_Src > $W_max); // 1 si vrai (true)
      }
      // ---------------------------------------------
      // REDIMENSIONNEMENT si la condition est vraie
      // ---------------------------------------------
      // - Si l'image Source est plus petite que les dimensions indiquées :
      // Par defaut : PAS de redimensionnement.
      // - Mais on peut "forcer" le redimensionnement en ajoutant ici :
      // $condition = 1; (risque de perte de qualité)
      if ($condition==1) {
         // ---------------------
         // creation de la ressource-image "Src" en fonction de l extension
         switch($extension_Src) {
         case 'jpg':
         case 'jpeg':
           $Ress_Src = imagecreatefromjpeg($rep_Src.$img_Src);
           break;
         case 'png':
           $Ress_Src = imagecreatefrompng($rep_Src.$img_Src);
           break;
         }
         // ---------------------
         // creation d une ressource-image "Dst" aux dimensions finales
         // fond noir (par defaut)
         switch($extension_Src) {
         case 'jpg':
         case 'jpeg':
           $Ress_Dst = imagecreatetruecolor($W,$H);
           break;
         case 'png':
           $Ress_Dst = imagecreatetruecolor($W,$H);
           // fond transparent (pour les png avec transparence)
           imagesavealpha($Ress_Dst, true);
           $trans_color = imagecolorallocatealpha($Ress_Dst, 0, 0, 0, 127);
           imagefill($Ress_Dst, 0, 0, $trans_color);
           break;
         }
         // ---------------------
         // REDIMENSIONNEMENT (copie, redimensionne, re-echantillonne)
         imagecopyresampled($Ress_Dst, $Ress_Src, 0, 0, 0, 0, $W, $H, $W_Src, $H_Src); 
         // ---------------------
         // ENREGISTREMENT dans le repertoire (avec la fonction appropriee)
         switch ($extension_Src) { 
         case 'jpg':
         case 'jpeg':
           imagejpeg ($Ress_Dst, $rep_Dst.$img_Dst);
           break;
         case 'png':
           imagepng ($Ress_Dst, $rep_Dst.$img_Dst);
           break;
         }
         // ------------------------
         // liberation des ressources-image
         imagedestroy ($Ress_Src);
         imagedestroy ($Ress_Dst);
      }
      // ------------------------
   }
 }
 // ---------------------------------------------------
 // retourne : true si le redimensionnement et l'enregistrement ont bien eu lieu, sinon false
 if ($condition==1 && file_exists($rep_Dst.$img_Dst)) { return true; }
 else { return false; }
 // ---------------------------------------------------
};
?>
<script type="text/javascript">
    function ClicBouton(url){
        document.form2.submit();

        document.form2.action = url;  
      
    }
</script>


